package com.ezjie.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.ezjie.baselib.core.base.BaseFragmentActivity;
import com.ezjie.baselib.util.AppUtil;
import com.ezjie.baselib.util.LogUtils;
import com.ezjie.baselib.util.NetworkUtil;
import com.ezjie.baselib.util.OnDialogBtnClickImpl;
import com.ezjie.login.core.base.BaseBean;
import com.ezjie.login.core.http.HttpRequestAbstractCallBack;
import com.ezjie.login.task.RegisterTask;
import com.ezjie.login.util.UmengPages;
import com.ezjie.login.widget.AppWarnDialog;
import com.ezjie.login.widget.LinearLayoutView;
import com.ezjie.login.widget.LinearLayoutView.KeyBordStateListener;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.umeng.analytics.MobclickAgent;

/**
 * 重置密码
 * @author renyangyang
 *
 */
//@ContentView(R.layout.layout_forget_password)
public class ResetPassActivity extends BaseFragmentActivity {
	
//	@ViewInject(R.id.login_back_btn)
	private ImageView login_back_btn;
//	@ViewInject(R.id.ll_root_view)
	private LinearLayoutView ll_root_view;
//	@ViewInject(R.id.login_logo)
	private ImageView login_logo;
//	@ViewInject(R.id.et_password)
	private EditText password;
//	@ViewInject(R.id.sure_btn)
	private Button sure_btn;
//	@ViewInject(R.id.iv_show_paw)
	private ImageView password_isshow;
	
	private String mobile;
	private String email;
	private String verify_code;
	private RegisterTask registerTask;
	private boolean isOpen = true;//判断密码是否显示
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_forget_password);
		login_back_btn = (ImageView)super.findViewById(R.id.login_back_btn);
		login_logo = (ImageView)super.findViewById(R.id.login_logo);
		password = (EditText)super.findViewById(R.id.et_password);
		sure_btn = (Button)super.findViewById(R.id.sure_btn);
		password_isshow = (ImageView)super.findViewById(R.id.iv_show_paw);
		ll_root_view = (LinearLayoutView)super.findViewById(R.id.ll_forget_pswd);
		
		ViewUtils.inject(this);
		
		registerTask = new RegisterTask(this);
		init();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_RESET_PASS);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_RESET_PASS);
	}
	
	private void init(){
		Intent intent = getIntent();
		mobile = intent.getStringExtra("mobile");
		email = intent.getStringExtra("email");
		verify_code = intent.getStringExtra("verify_code");
		
		
		login_back_btn.setOnClickListener(this);
		sure_btn.setOnClickListener(this);
		password_isshow.setOnClickListener(this);
		password.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				String string = s.toString();
				if (TextUtils.isEmpty(string)||string.length()<6) {
					sure_btn.setEnabled(false);
				}else {
					sure_btn.setEnabled(true);
				}
			}
		});
		ll_root_view.setKeyBordStateListener(new KeyBordStateListener() {
			
			@Override
			public void stateChange(int state) {
				switch (state) {
				case LinearLayoutView.KEYBORAD_HIDE:
					login_logo.setVisibility(View.VISIBLE);
					break;
				case LinearLayoutView.KEYBORAD_SHOW:
					login_logo.setVisibility(View.GONE);
					break;
				}
				
			}
		});
	}

	@Override
	public void onClickEvent(View view) {
		if(view.getId() == R.id.login_back_btn){
			this.finish();
		}else if(view.getId() == R.id.sure_btn){
			doSubmit();
		}else  if (view.getId() == R.id.iv_show_paw){
			//密码是否显示
			if (isOpen) {
				//显示密码
				isOpen = false;
				password_isshow.setImageResource(R.drawable.paw_hidder);
				password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			}else {//隐藏密码
				isOpen = true;
				password_isshow.setImageResource(R.drawable.paw_show);
				password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
		}else{
			
		}


	}
	
	private void doSubmit(){
		if (!NetworkUtil.isNetworkAvailable(this)) {
			AppUtil.showToast(this, R.string.no_network);
			return;
		}
		String new_password = password.getText().toString();
		if (TextUtils.isEmpty(new_password)) {
			AppUtil.showToast(this, "请输入新的密码");
			return;
		}
		if (!AppUtil.passwordFormat(new_password)) {
			AppUtil.showToast(this, R.string.login_input_correct_password);
			return;
		}
		
		registerTask.resetPass(new_password, verify_code, mobile, email, new HttpRequestAbstractCallBack(this,false) {
			@Override
			public void onStartCallBack() {
				// TODO Auto-generated method stub
				super.onStartCallBack();
				AppUtil.showProgressDialog(ResetPassActivity.this);
			}
			@Override
			public void onSuccessCallBack(String result) {
				// TODO Auto-generated method stub
				super.onSuccessCallBack(result);
				AppUtil.dismissProgressDialog();
				LogUtils.d(result);
				try {
					BaseBean baseBean = JSON.parseObject(result, BaseBean.class);
					if (baseBean != null) {
						if (baseBean.getStatus_code()==200) {
							//密码修改成功  到登录界面
							AppUtil.showToast(ResetPassActivity.this, R.string.reset_paw_success);
							ResetPassActivity.this.setResult(RESULT_OK);
							ResetPassActivity.this.finish();
							
						}else {
							String msg = baseBean.getMsg();
							if (TextUtils.isEmpty(msg)) {
								msg = ResetPassActivity.this.getResources().getString(R.string.reg_fail);
							}
							final AppWarnDialog appWarnDialog = new AppWarnDialog(ResetPassActivity.this, R.style.customDialog);
							appWarnDialog.show();
							appWarnDialog.setOneButton(true);
							appWarnDialog.setMessage(msg);
							appWarnDialog.setKnowButton(R.string.confirm);
							appWarnDialog.setListener(new OnDialogBtnClickImpl() {
								public void onKnowClick() {
									appWarnDialog.dismiss();
								}
							});
						}
					}
				} catch (Exception e) {
					LogUtils.exception(e);
				}
			}
			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				// TODO Auto-generated method stub
				super.onFailureCallBack(error, msg);
				AppUtil.dismissProgressDialog();
        		AppUtil.showToast(ResetPassActivity.this, R.string.load_net_data_failure);
			}
		});
	}

}
